<template>
  <div class="cards">
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane label="凹版印刷" name="first">
        <div style="max-width: 950px; margin: 0 auto">
          <div class="ipt">￥ {{ TotalPrice }}</div>
          <el-form>
            <el-row>
              <el-col :span="8">
                <el-form-item labelWidth="100px" label="制袋袋型">
                  <el-select v-model="form.daixing" @change="daix">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in daixing"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="zhidaiPrice">
                    制袋费：￥{{ zhidaiPrice.toFixed(4) }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="数量">
                  <div class="fix">
                    <el-input
                      v-model="form.num"
                      @input="numChange"
                      placeholder="请填写"
                    ></el-input
                    >只
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="宽度">
                  <div class="fix">
                    <el-input
                      :disabled="dwFalse"
                      @input="dkChange"
                      type="Number"
                      v-model="form.dk"
                      placeholder="请填写"
                    ></el-input>
                    mm
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="高度">
                  <div class="fix">
                    <el-input
                      :disabled="dhFalse"
                      @input="dgChange"
                      type="Number"
                      v-model="form.dg"
                      placeholder="请填写"
                    ></el-input>
                    mm
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="插底">
                  <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="插底是展开尺寸"
                    placement="top-start"
                  >
                    <div class="fix">
                      <el-input
                        @input="chaChange"
                        v-model="form.cd"
                        placeholder="请填写"
                      ></el-input>
                      mm
                    </div>
                  </el-tooltip>
                </el-form-item>
                <el-form-item labelWidth="100px" label="材料1">
                  <div class="fix">
                    <el-select
                      style="width: 150px"
                      v-model="form.ca1"
                      @change="ca1s"
                    >
                      <el-option
                        :value="i.materialName"
                        :label="i.materialName"
                        v-for="(i, s) in tableData"
                        :key="s"
                      />
                    </el-select>
                    <el-input
                      @input="caa1"
                      v-model="thick.a"
                      style="width: 50px; margin-left: 5px; margin-right: 5px"
                    ></el-input
                    >μm
                  </div>
                  <div style="color: red" v-if="caidanprice1">
                    每平方米价格：￥{{ caidanprice1 }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="材料2">
                  <div class="fix">
                    <el-select
                      style="width: 150px"
                      v-model="form.ca2"
                      @change="ca2s"
                    >
                      <el-option
                        :value="i.materialName"
                        :label="i.materialName"
                        v-for="(i, s) in tableData"
                        :key="s"
                      />
                    </el-select>
                    <el-input
                      @input="caa2"
                      v-model="thick.b"
                      style="width: 50px; margin-left: 5px; margin-right: 5px"
                    ></el-input
                    >μm
                  </div>
                  <div style="color: red" v-if="caidanprice2">
                    每平方米价格：￥{{ caidanprice2 }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="材料3">
                  <div class="fix">
                    <el-select
                      style="width: 150px"
                      v-model="form.ca3"
                      @change="ca3s"
                    >
                      <el-option
                        :value="i.materialName"
                        :label="i.materialName"
                        v-for="(i, s) in tableData"
                        :key="s"
                      />
                    </el-select>
                    <el-input
                      @input="caa3"
                      v-model="thick.c"
                      style="width: 50px; margin-left: 5px; margin-right: 5px"
                    ></el-input
                    >μm
                  </div>
                  <div style="color: red" v-if="caidanprice3">
                    每平方米价格：￥{{ caidanprice3 }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="材料4">
                  <div class="fix">
                    <el-select
                      style="width: 150px"
                      v-model="form.ca4"
                      @change="ca4s"
                    >
                      <el-option
                        :value="i.materialName"
                        :label="i.materialName"
                        v-for="(i, s) in tableData"
                        :key="s"
                      />
                    </el-select>
                    <el-input
                      @input="caa4"
                      v-model="thick.d"
                      style="width: 50px; margin-left: 5px; margin-right: 5px"
                    ></el-input
                    >μm
                  </div>
                  <div style="color: red" v-if="caidanprice4">
                    每平方米价格：￥{{ caidanprice4 }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="材料5">
                  <div class="fix">
                    <el-select
                      style="width: 150px"
                      v-model="form.ca5"
                      @change="ca5s"
                    >
                      <el-option
                        :value="i.materialName"
                        :label="i.materialName"
                        v-for="(i, s) in tableData"
                        :key="s"
                      />
                    </el-select>
                    <el-input
                      @input="caa5"
                      v-model="thick.e"
                      style="width: 50px; margin-left: 5px; margin-right: 5px"
                    ></el-input
                    >μm
                  </div>
                  <div style="color: red" v-if="caidanprice5">
                    每平方米价格：￥{{ caidanprice5 }}
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item labelWidth="100px" label="印刷">
                  <el-select v-model="form.yinshua" @change="yinChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in yinshua"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="yinPrice">
                    价格：￥{{ yinPrice}}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="复合">
                  <el-select v-model="form.fuhe" @change="fuChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in fuhe"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="fuhePrice">
                    价格：￥{{ fuhePrice}}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="系数">
                  <el-select v-model="form.xishu" @change="tpss">
                    <el-option
                      :value="i.bagName"
                      :label="i.bagName"
                      v-for="(i, s) in factorData"
                      :key="s"
                    ></el-option>
                  </el-select>
                  <div v-if="xis" style="color: red">
                    系数值：{{ (xis + ke).toFixed(2) }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="拉链">
                  <el-select v-model="form.lalian" @change="lals">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in lalian"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="laPrice">
                    价格：￥{{ laPrice }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="气阀">
                  <el-select v-model="form.qifa" @change="qiChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in qifa"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="qiPrcie">
                    价格：￥{{ qiPrcie }}
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="吸嘴">
                  <el-select v-model="form.xizui" @change="xizuiChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in qizui"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="xiPrice">
                    价格：￥{{ xiPrice }}
                  </div>
                </el-form-item>

                <el-form-item labelWidth="100px" label="特殊工艺">
                  <el-select v-model="form.tesu" @change="teChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in tesugongyi"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="tejai">价格：￥{{ tejai }}</div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="可降解">
                  <el-select v-model="form.ke" @change="keChange">
                    <el-option
                      :value="i.materialName"
                      :label="i.materialName"
                      v-for="(i, s) in tableDataJIANG"
                      :key="s"
                    />
                  </el-select>
                  <div v-if="ke" style="color: red">可降解加：{{ ke }}</div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="开机费">
                  <el-select v-model="form.kaiji" @change="kaiChange">
                    <el-option
                      :value="i.name"
                      :label="i.name"
                      v-for="(i, s) in kaijifei"
                      :key="s"
                    />
                  </el-select>
                  <div style="color: red" v-if="kaijiPrice">
                    价格：￥{{ kaijiPrice }}
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item labelWidth="100px" label="单只面积">
                  <div class="fix">
                    <el-input v-model="form.dm" disabled></el-input> m²
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="单面总厚度">
                  <div class="fix">
                    <el-input v-model="form.dz"></el-input>mm
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="总面积">
                  <div class="fix">
                    <el-input v-model="form.mj" disabled></el-input>m²
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="单只克重">
                  <div class="fix">
                    <el-input v-model="form.dj"></el-input>g
                  </div>
                </el-form-item>

                <el-form-item labelWidth="100px" label="总重量">
                  <div class="fix">
                    <el-input v-model="form.zl"></el-input>g
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="单价">
                  <div class="fix">
                    <el-input v-model="form.djia"></el-input>元
                  </div>
                </el-form-item>
                <el-form-item labelWidth="100px" label="发票">
                  <el-select v-model="form.fapiao" @change="faChange">
                    <el-option
                      :value="i.value"
                      :label="i.name"
                      v-for="(i, s) in fapiao"
                      :key="s"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item labelWidth="100px" label="附加费用">
                  <div class="fix">
                    <el-input
                      @input="fujiaChange"
                      v-model="form.fujia"
                      placeholder="请输入"
                    ></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="btn">
            <el-button @click="CloseAdd()">清空</el-button>
            <el-button
              style="background: #7052ff; color: #fff"
              @click="handleSaveAdd()"
              >计算</el-button
            >
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="数码印刷" name="second">
        <div style="max-width: 800px; margin: 0 auto">
          <div class="ipt">￥ {{ TotalPrice1 }}</div>
          <el-form :model="dictForm">
            <el-row>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="袋型">
                  <el-select v-model="dictForm.dict" @change="groupedDataClick">
                    <el-option
                      v-for="item in Object.keys(groupedData)"
                      :key="item"
                      :value="item"
                      :label="item.materialName"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="尺寸">
                  <el-select v-model="dictForm.size" @change="chase">
                    <el-option
                      v-for="item in groupedData[selectedName]"
                      :value="item.size"
                      :label="item.size"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="咖啡豆装量">
                  <el-select v-model="dictForm.houdu" @change="kafi">
                    <el-option
                      v-for="item in groupedData[selectedName]"
                      :value="item.coffeeBeanWeight"
                      :label="item.coffeeBeanWeight"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="厚度">
                  <el-input v-model="dictForm.thickness" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="数量">
                  <el-input
                    type="Number"
                    @input="totalNum"
                    v-model="dictForm.num"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="最低起订金额">
                  <el-input
                    type="Number"
                    v-model="dictForm.price"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item labelWidth="100px" label="单价">
                  <el-input
                    type="Number"
                    v-model="dictForm.unit"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item labelWidth="100px" label="附加费用">
                  <el-input
                    @input="resPriceChange"
                    type="Number"
                    v-model="dictForm.resPrice"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="btn">
            <el-button @click="Close()">清空</el-button>
            <el-button
              style="background: #7052ff; color: #fff"
              @click="SaveAdd()"
              >计算</el-button
            >
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="丝网和烫金" name="third">
        <div style="max-width: 800px; margin: 0 auto">
          <div class="ipt">￥ {{ TotalPrice2 }}</div>
          <el-form labelWidth="100px" :model="SelectedValues">
            <el-row>
              <el-col :span="12">
                <el-form-item label="起订量">
                  <el-select
                    v-model="selectedValues.quantity"
                    @change="updatePrice"
                    placeholder="请选择起订量"
                  >
                    <el-option
                      v-for="item in uniqueValues.quantity"
                      :key="item"
                      :label="item.toString()"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="印刷区域">
                  <el-select
                    v-model="selectedValues.printArea"
                    @change="updatePrice"
                    placeholder="请选择印刷区域"
                  >
                    <el-option
                      v-for="item in uniqueValues.printArea"
                      :key="item"
                      :label="item.toString()"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="logo颜色">
                  <el-select
                    v-model="selectedValues.logoColor"
                    @change="updatePrice"
                    placeholder="请选择logo颜色"
                  >
                    <el-option
                      v-for="item in uniqueValues.logoColor"
                      :key="item"
                      :label="item.toString()"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="印刷面数">
                  <el-select
                    v-model="selectedValues.printSides"
                    @change="updatePrice"
                    placeholder="请选择印刷面数"
                  >
                    <el-option
                      v-for="item in uniqueValues.printSides"
                      :key="item"
                      :label="item.toString()"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="印刷单价">
                  <div class="fix">
                    <el-input disabled v-if="!matchedData"></el-input>
                    <el-input
                      disabled
                      v-else
                      v-model="matchedData.unitPrice"
                    ></el-input>
                    元
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="印刷总价">
                  <div class="fix">
                    <el-input disabled v-if="!matchedData"></el-input>
                    <el-input
                      disabled
                      v-else
                      v-model="matchedData.totalPrice"
                    ></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="袋子单价">
                  <div class="fix">
                    <el-input
                      @input="calculateSum"
                      v-model="matc.daidan"
                      placeholder="请输入"
                    ></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="运费">
                  <div class="fix">
                    <el-input v-model="matc.yun" placeholder="请输入"></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="总单价">
                  <div class="fix">
                    <el-input
                      v-model="matc.zong"
                      placeholder="请输入"
                    ></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="附加费用">
                  <div class="fix">
                    <el-input v-model="matc.fu" placeholder="请输入"></el-input
                    >元
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="btn">
            <el-button @click="CloseAdd(), (TotalPrice2 = 0)">清空</el-button>
            <el-button
              style="background: #7052ff; color: #fff"
              @click="siwangClick()"
              >计算</el-button
            >
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts">
import {
  defineAsyncComponent,
  onMounted,
  watch,
  watchEffect,
  reactive,
  computed,
  ref,
} from "vue";
import type { TabsPaneContext } from "element-plus";
import { ElMessage, ElMessageBox, FormRules, FormInstance } from "element-plus";
const Card = defineAsyncComponent(() => import("@/components/cards/index.vue"));
import { User, Plus, Warning, QuestionFilled } from "@element-plus/icons-vue";
import {
  factorList,
  dictTypeList,
  dictList,
  dict,
  list,
  bronzingList,
  productprices,
} from "@/api/index";
import { tr } from "node_modules/element-plus/es/locale";

const activeName = ref("first");

const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event);
  // TotalPrice.value = 0
};
const TotalPrice = ref(0);
const TotalPrice1 = ref(0);
const TotalPrice2 = ref(0);

const shows = ref(false);
const tabs = ref([]);
const xisu = ref({});

const chaChange = () => {
  addsT();
};
const fujiaChange = (val: any) => {
  addsT();
};
const tpss = (val: any) => {
  const index = factorData.value.findIndex((item) => item.bagName == val);
  // xisu.value = factorData.value[index]
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
  if (form.value.mj < 4000) {
    console.log(
      "-------小于四千--------",
      xisu.value.areaRange1,
      factorData.value[index].areaRange1
    );
    xis.value = factorData.value[index].areaRange1;
    su.value = Number(ke.value) + Number(xis.value);
  }
  if (Number(form.value.mj) > 4000 && Number(form.value.mj) < 6000) {
    xis.value = factorData.value[index].areaRange2;
    su.value = Number(ke.value) + Number(xis.value);
    console.log(
      "-------大于四千小于六千--------",
      xisu.value.areaRange2,
      factorData.value[index].areaRange2
    );
  }
  if (Number(form.value.mj) > 6000 && Number(form.value.mj) < 8000) {
    xis.value = factorData.value[index].areaRange3;
    su.value = Number(ke.value) + Number(xis.value);
    console.log(
      "-------大于六千小于八千--------",
      xisu.value.areaRange3,
      factorData.value[index].areaRange3
    );
  }
  if (Number(form.value.mj) > 8000) {
    xis.value = factorData.value[index].areaRange4;
    su.value = Number(ke.value) + Number(xis.value);
    console.log(
      "-------大于八千--------",
      xisu.value.areaRange4,
      factorData.value[index].areaRange4
    );
  }
  handleSaveAdd();
};

const resPriceChange = (val: any) => {
  addsT();
};
//监听计算
const muss = computed(() => matc.value.daidan * selectedValues.value.quantity);
const form = ref({
  xishu: "",
  fujia: 0,
  mj: 0,
  dm: 0,
  dj: 0,
  dz: 0,
  num: 1,
  dk: "",
  dg: "",
  ms: "",
  ca1: "",
  ca2: "",
  ca3: "",
  ca4: "",
  ca5: "",
  zl: 0,
  cd: "",
  djia: 0, //单价
  lalian: "",
  xizui: "",
  qifa: "",
  fapiao: "1",
  ke: "",
  tesu: "",
  fuhe: "",
  yinshua: "",
  daixing: "",
});

const thick = ref({
  a: "",
  b: "",
  c: "",
  d: "",
  e: "",
  aa: "",
  bb: "",
  cc: "",
  dd: "",
  ee: "",
});
const xishuzhi = ref(0);
const KAINUM = ref(10000);
const daxingValue = ref("");
const daix = (val: any) => {
  const index = daixing.value.findIndex((item) => item.name === val);
  daxingValue.value = daixing.value[index].value;
  kaijifei.value.forEach((item) => {
    if (item.name.includes(daixing.value[index].name)) {
      form.value.kaiji = item.name;
      kaijiPrice.value = item.value / Number(KAINUM.value);
      console.log("ssssssss", kaijiPrice.value, "00000", KAINUM.value);
    }
  });
  factorData.value.forEach((item) => {
    if (item.bagName.includes(daixing.value[index].name)) {
      form.value.xishu = item.bagName;
      xisu.value = item;
      xis.value = item.areaRange1;
    }
  });
  if (["三边封袋", "自立袋", "八边封"].includes(form.value.daixing)) {
    laPrice.value = (Number(form.value.dk) / 1000) * Number(laM.value);
    laPrice.value = truncateDecimals(laPrice.value, 2);
    lake.value = (Number(lake.value) * Number(form.value.dk)) / 1000;
  }
  if (form.value.daixing == "四边封+拉链") {
    laPrice.value = Number(form.value.dk) * 0.001;
    laPrice.value = truncateDecimals(laPrice.value, 2);
    lake.value = (Number(lake.value) * Number(form.value.dk)) / 1000;
  }

  if (
    !["三边封袋", "自立袋", "八边封", "四边封 + 拉链"].includes(
      form.value.daixing
    )
  ) {
    laPrice.value = 0;
  }

  addsT();
};
//拉链
const laM = ref(0);
const laPrice = ref(0);
const lake = ref(0);

const lals = (val: any) => {
  const index = lalian.value.findIndex((item) => item.name === val);
  laM.value = Number(lalian.value[index].value);
  console.log('打印拉链',laM.value)
  console.log('大大大的')
  if (!form.value.daixing)
    return ElMessage({ type: "warning", message: "请选择袋型" });
  if (!form.value.dk)
    return ElMessage({ type: "warning", message: "请输入宽度！" });
  // 重量克/米
  if (val === "普通拉链") {
    lake.value = 4.957;
  } else {
    lake.value = 10.537;
  }

  if (["三边封袋", "自立袋", "八边封"].includes(form.value.daixing)) {
    laPrice.value = (Number(form.value.dk) / 1000) * Number(laM.value);
    laPrice.value = truncateDecimals(laPrice.value, 2);
    lake.value = Number(lake.value) * (Number(form.value.dk) / 1000);
  }
  if (form.value.daixing == "四边封+拉链") {
    laPrice.value = Number(form.value.dk) * 0.001;
    laPrice.value = truncateDecimals(laPrice.value, 2);
    lake.value = Number(lake.value) * (Number(form.value.dk) / 1000);
  }

  if (
    !["三边封袋", "自立袋", "八边封", "四边封 + 拉链"].includes(
      form.value.daixing
    )
  ) {
    laPrice.value = 0;
  }
  lake.value = truncateDecimals(lake.value, 6);
  addsT();
};
const dwFalse = ref(false);
const dhFalse = ref(false);
//吸嘴
const xiPrice = ref(0);
const xiKe = ref(0);
const xizuiChange = (val: any) => {
  const index = qizui.value.findIndex((item) => item.name === val);
  xiPrice.value = Number(qizui.value[index].value);

  if (val == "无吸嘴") {
    xiKe.value = 0;
  } else if (val == "普通8.6mm") {
    xiKe.value = 3;
  } else if (val == "普通10mm") {
    xiKe.value = 3.5;
  } else if (val == "普通13mm") {
    xiKe.value = 6;
  } else if (val == "普通16mm") {
    xiKe.value = 9;
  } else if (val == "普通22mm") {
    xiKe.value = 11;
  } else if (val == "普通33mm") {
    xiKe.value = 16.5;
  }
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  addsT();
};
//开票
const faPrice = ref(1);
const faChange = (val: any) => {
  const index = fapiao.value.findIndex((item) => item.name === val);
  console.log("开票", val);
  faPrice.value = val;
  const dgss =
    Number(form.value.djia) * Number(form.value.num) + Number(form.value.fujia);
  TotalPrice.value = truncateDecimals(dgss, 4);
  addsT();
};
//复合
const fuhePrice = ref(0);
const chen = ref(0);
const fuChange = (val: any) => {
  const index = fuhe.value.findIndex((item) => item.name === val);
  fuhePrice.value = Number(fuhe.value[index].value);
  console.log('符合打印', index , '---', fuhe.value[index].value ,fuhePrice.value)

  if (val == "常规三次") {
    chen.value = 2;
  } else if (val === "高温蒸煮三次") {
    chen.value = 2;
  } else if (val == "抗酒精三次") {
    chen.value = 2;
  } else {
    chen.value = 1;
  }
  addsT();
};
//开机费用
const kaijiPrice = ref(0);
const kaiChange = (val: any) => {
  const index = kaijifei.value.findIndex((item) => item.name === val);
  kaijiPrice.value = Number(kaijifei.value[index].value) / Number(KAINUM.value);
  console.log("开机费", kaijiPrice.value, "00000", KAINUM.value);
};

const keChange = (val: any) => {
  console.log("val", val);
  if (val == "无") return (ke.value = 0);
  ke.value = 0.05;
  addsT();
};

const yinChangeIndex = ref(0)
//印刷
const yinPrice = ref(0);
const yinChange = (val: any) => {
  const index = yinshua.value.findIndex((item) => item.name === val);
  yinChangeIndex.value = index
  console.log('yinshua.value[index].value',yinshua.value[index].value)
  if(form.value.dm<=0){
    return ElMessage({message:'请先计算面积！'})
  }
  yinPrice.value = Number(yinshua.value[index].value) * Number(form.value.dm);
  
  addsT();
};
//气阀
const qiPrcie = ref(0);
const qiKe = ref(0);
const qiChange = (val: any) => {
  const index = qifa.value.findIndex((item) => item.name === val);
  qiPrcie.value = Number(qifa.value[index].value);
  if (val === "无阀") {
    qiKe.value = 0;
  } else {
    qiKe.value = 1.5;
  }
  console.log("气阀价格", qiPrcie.value, "克重", qiKe.value);
  addsT();
};

const tePrice = ref(0);
const tejai = ref(0); //特殊工艺单价
const teChange = (val: any) => {
  const index = tesugongyi.value.findIndex((item) => item.name === val);
  tePrice.value = tesugongyi.value[index].value;
  console.log("特殊工艺", tePrice.value);
  //特殊工艺
  if (["哑油", "磨砂哑油", "触感哑油"].includes(val)) {
    tejai.value =
      Number(tePrice.value) *
      (((Number(form.value.dk) / 1000) * Number(form.value.dg)) / 1000);
    tejai.value = truncateDecimals(tejai.value, 2);
  } else if (["开窗"].includes(form.value.tesu)) {
    if (!form.value.dk)
      return ElMessage({ type: "warning", message: "请输入宽度！" });
    tejai.value = (Number(tePrice.value) * Number(form.value.dk)) / 1000;
    tejai.value = truncateDecimals(tejai.value, 2);
  } else {
    tejai.value = Number(tePrice.value);
  }
  addsT();
};

const dxPrice = ref(0);
const mj = ref(0);
const ke = ref(0);
const xis = ref(0);
const su = ref(0);
const go = ref(0);
const kais = ref(0);
const laNum = ref(0);
const xizuiNum = ref(0);
const faNum = ref(0);

const zhidaiPrice = ref(0); //制袋费
const addsT = () => {
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  if (form.value.ke != "") ke.value = 0.05;
  if (form.value.ke == "") ke.value = 0;
  const funor = Number(fuhePrice.value) * Number(chen.value);
  const rty = Number(
    caidanprice1.value +
      caidanprice2.value +
      caidanprice3.value +
      caidanprice4.value +
      caidanprice5.value
  ).toFixed(6);
  const aa =
    Number(rty) * Number(form.value.dm) +
    Number(yinPrice.value) +
    Number(funor);
  const one =
    Number(xiPrice.value) + Number(laPrice.value) + Number(qiPrcie.value);
  const typs = Number(aa) * (Number(xis.value) + Number(ke.value));
  const two = typs.toFixed(6);
  let three = 0;
  if (form.value.mj <= 4000) {
    KAINUM.value = 10000;
    const kaiPrice = Number(kaijiPrice.value);
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value) +
      Number(kaiPrice);
  }
  if (form.value.mj > 4000) {
    KAINUM.value = 0;
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value);
  }
  const four = Number(three);
  const typ = Number(four) * Number(form.value.fapiao);
  form.value.djia = truncateDecimals(typ, 4);
  const dgss =
    Number(form.value.djia) * Number(form.value.num) + Number(form.value.fujia);
  TotalPrice.value = truncateDecimals(dgss, 4);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
};
const handleSaveAdd = () => {
  //先计算袋型价格
  //单只克重
  // if (!form.value.daixing) return ElMessage({ type: 'warning', message: '请选择袋型!' })

  // if (form.value.num<=0) return ElMessage({ type: 'warning', message: '请输入数量！' })

  if (!form.value.dk)
    return ElMessage({ type: "warning", message: "请输入宽度！" });
  if (!form.value.dg)
    return ElMessage({ type: "warning", message: "请输入高度！" });

  // 重量 = ’=面积*（各材料平方克重+3g/平方*复合次数）+拉链重量+气阀重量+吸嘴重量
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);

  if (daxingValue.value.split("*")[0] === "W") {
    dxPrice.value =
      Number(form.value.dk) * Number(daxingValue.value.split("*")[1]);
  }
  if (daxingValue.value.split("*")[0] == "H") {
    dxPrice.value =
      Number(form.value.dg) * Number(daxingValue.value.split("*")[1]);
  }

  // 可降解
  if (form.value.ke != "") ke.value = 0.05;
  if (form.value.ke == "") ke.value = 0;

  //系数价格

  console.log("系数11111", xis.value, "可溶解系数加" + ke.value);

  // 单只价格
  // 印刷价格
  // 复合价格 3层材料结构，复合2次
  const funor = Number(fuhePrice.value) * Number(chen.value);
  const rty = Number(
    caidanprice1.value +
      caidanprice2.value +
      caidanprice3.value +
      caidanprice4.value +
      caidanprice5.value
  ).toFixed(6);
  const aa =
    Number(rty) * Number(form.value.dm) +
    Number(yinPrice.value) +
    Number(funor);
  const one =
    Number(xiPrice.value) + Number(laPrice.value) + Number(qiPrcie.value);
  const typs = Number(aa) * (Number(xis.value) + Number(ke.value));
  const two = typs.toFixed(6);
  console.log("复合价格----", aa, "////", two);

  let three = 0;
  if (form.value.mj <= 4000) {
    KAINUM.value = 10000;
    const kaiPrice = Number(kaijiPrice.value);
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value) +
      Number(kaiPrice);
  }
  if (form.value.mj > 4000) {
    KAINUM.value = 0;
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value);
  }
  const four = Number(three);
  console.log("9090909", KAINUM.value);

  // 拉链价格+气阀价格+吸嘴价格+印刷价格+复合价格

  console.log(
    "---------材料成本--------",
    aa,
    aa.toFixed(2),
    "99897",
    zhidaiPrice.value,
    tejai.value,
    "----"
  );
  console.log(
    "-----------------",
    two,
    Number(kaijiPrice.value),
    "ssss",
    kaijiPrice.value
  );
  console.log("---------22222单价--------", three);
  console.log("---------1111单价2--------", four, kaijiPrice.value);
  const typ = Number(four) * Number(form.value.fapiao);
  form.value.djia = truncateDecimals(typ, 4);
  const dgss =
    Number(form.value.djia) * Number(form.value.num) + Number(form.value.fujia);
  TotalPrice.value = truncateDecimals(dgss, 4);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
};

const truncateDecimals = (num: any, decimalPlaces: any) => {
  const factor = Math.pow(10, decimalPlaces);
  return Math.floor(num * factor) / factor;
};
//监听输入袋子宽度
const dkChange = () => {
  if (!form.value.daixing)
    return ElMessage({ type: "warning", message: "请选择袋型" });
  tejai.value = (Number(tePrice.value) * Number(form.value.dk)) / 1000;
  tejai.value = truncateDecimals(tejai.value, 2);
  if (["三边封袋", "自立袋", "八边封"].includes(form.value.daixing)) {
    laPrice.value = (Number(form.value.dk) / 1000) * Number(laM.value);
    laPrice.value = truncateDecimals(laPrice.value, 2);
    console.log("拉链价格", laPrice.value + "元");
    lake.value = (Number(lake.value) * Number(form.value.dk)) / 1000;
  }
  if (form.value.daixing == "四边封+拉链") {
    laPrice.value = Number(form.value.dk) * 0.001;
    laPrice.value = truncateDecimals(laPrice.value, 2);
    console.log("四边封+拉链", laPrice.value + "元");
    lake.value = (Number(lake.value) * Number(form.value.dk)) / 1000;
  }

  if (
    !["三边封袋", "自立袋", "八边封", "四边封 + 拉链"].includes(
      form.value.daixing
    )
  ) {
    laPrice.value = 0;
  }

  const Ms = ref(0);
  const dk = Number(form.value.dk) / 1000;
  const dg = Number(form.value.dg) / 1000;
  const cd = Number(form.value.cd) / 1000;
  if (form.value.daixing == "三边封袋") {
    Ms.value = dk * dg * 2;
    zhidaiPrice.value = dk * 0.2;
  }
  if (form.value.daixing == "三边封中封袋") {
    Ms.value = (dk + 0.015) * dg * 2;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "自立袋") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });

    Ms.value = (dg * 2 + cd) * dk;
    zhidaiPrice.value = dk * 0.2;
  }
  if (form.value.daixing == "八边封") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * (0.5 * cd + dg);
    zhidaiPrice.value = dk * 1;
  }
  if (form.value.daixing == "卷膜") {
    Ms.value = dk * dk;
    zhidaiPrice.value = 0;
  }
  if (form.value.daixing == "风琴袋") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd + 0.015) * 2 * dg;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "四边封") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * dg;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "中封袋") {
    Ms.value = (dk + 0.015) * 2 * (dg / 1000);
    zhidaiPrice.value = dg * 0.2;
    console.log("中封袋---", Ms.value);
  }
  if (form.value.daixing == "四边封+拉链") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * dg;
    zhidaiPrice.value = dk * 1;
  }
  form.value.dm = truncateDecimals(Ms.value, 4);
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
  if (form.value.mj <= 4000) {
    KAINUM.value = 10000;
  }
  if (form.value.mj > 4000) {
    KAINUM.value = 0;
  }
  addsT();
};
const dgChange = () => {
  if (!form.value.daixing)
    return ElMessage({ type: "warning", message: "请选择袋型" });

  const Ms = ref(0);
  const dk = Number(form.value.dk) / 1000;
  const dg = Number(form.value.dg) / 1000;
  const cd = Number(form.value.cd) / 1000;
  if (form.value.daixing == "三边封袋") {
    Ms.value = dk * dg * 2;
    zhidaiPrice.value = dk * 0.2;
  }
  if (form.value.daixing == "三边封中封袋") {
    Ms.value = (dk + 0.015) * dg * 2;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "自立袋") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });

    Ms.value = (dg * 2 + cd) * dk;
    zhidaiPrice.value = dk * 0.2;
  }
  if (form.value.daixing == "八边封") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * (0.5 * cd + dg);
    zhidaiPrice.value = dk * 1;
  }
  if (form.value.daixing == "卷膜") {
    Ms.value = dk * dk;
    zhidaiPrice.value = 0;
  }
  if (form.value.daixing == "风琴袋") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd + 0.015) * 2 * dg;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "中封袋") {
    Ms.value = (dk + 0.015) * 2 * (dg / 1000);
    zhidaiPrice.value = dg * 0.2;
    console.log("中封袋5555---", Ms.value);
  }
  if (form.value.daixing == "四边封") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * dg;
    zhidaiPrice.value = dg * 0.2;
  }
  if (form.value.daixing == "四边封+拉链") {
    if (!form.value.cd)
      return ElMessage({ type: "warning", message: "请输入插底参数" });
    Ms.value = (dk + cd) * 2 * dg;
    zhidaiPrice.value = dk * 1;
  }
  form.value.dm = truncateDecimals(Ms.value, 4);
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
  console.log("总面积", form.value.mj,abc);
  if (form.value.mj <= 4000) {
    KAINUM.value = 10000;
  }
  if (form.value.mj > 4000) {
    KAINUM.value = 0;
  }
  addsT();
};

const numChange = (val: any) => {
  if (!form.value.daixing)
    return ElMessage({ type: "warning", message: "请选择袋型" });
  const abc = Number(form.value.dm) * Number(form.value.num);
  form.value.mj = truncateDecimals(abc, 4);
  if (form.value.mj < 4000) {
    console.log("---------------", xisu.value);
    xis.value = xisu.value.areaRange1;
    su.value = Number(ke.value) + Number(xis.value);
  }
  if (Number(form.value.mj) > 4000 && Number(form.value.mj) < 6000) {
    xis.value = xisu.value.areaRange2;
    su.value = Number(ke.value) + Number(xis.value);
  }
  if (Number(form.value.mj) > 6000 && Number(form.value.mj) < 8000) {
    xis.value = xisu.value.areaRange3;
    su.value = Number(ke.value) + Number(xis.value);
  }
  if (Number(form.value.mj) > 8000) {
    xis.value = xisu.value.areaRange4;
    su.value = Number(ke.value) + Number(xis.value);
  }
  const funor = Number(fuhePrice.value) * Number(chen.value);
  const rty = Number(
    caidanprice1.value +
      caidanprice2.value +
      caidanprice3.value +
      caidanprice4.value +
      caidanprice5.value
  ).toFixed(6);
  const aa =
    Number(rty) * Number(form.value.dm) +
    Number(yinPrice.value) +
    Number(funor);
  const one =
    Number(xiPrice.value) + Number(laPrice.value) + Number(qiPrcie.value);
  const typs = Number(aa) * (Number(xis.value) + Number(ke.value));
  const two = typs.toFixed(6);
  console.log("复合价格----", aa, "////", two);

  let three = 0;
  if (form.value.mj <= 4000) {
    KAINUM.value = 10000;
    const kaiPrice = Number(kaijiPrice.value);
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value) +
      Number(kaiPrice);
  }
  if (form.value.mj > 4000) {
    KAINUM.value = 0;
    three =
      Number(two) +
      Number(zhidaiPrice.value) +
      Number(one) +
      Number(tejai.value);
  }
  const four = Number(three);
  // 拉链价格+气阀价格+吸嘴价格+印刷价格+复合价格
  const typ = Number(four) * Number(form.value.fapiao);
  form.value.djia = truncateDecimals(typ, 4);
  const dgss =
    Number(form.value.djia) * Number(form.value.num) + Number(form.value.fujia);
  TotalPrice.value = truncateDecimals(dgss, 4);
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};

const caidanprice1 = ref(0); //材料单价
const caike1 = ref(0); //材料重量
const caa1 = (val: any) => {
  const density = Number(density1.value); // g/cm³
  const thickness = Number(thick.value.a); // μm
  const pricePerGram = Number(pricePerGram1.value);
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike1.value = gramPerSquareMeter;
  caidanprice1.value = Number(roundedPrice);
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const density1 = ref("");
const pricePerGram1 = ref("");
const ca1s = (val: any) => {
  const index = tableData.value.findIndex((item) => item.materialName === val);
  thick.value.a = tableData.value[index].thickness;
  const density = tableData.value[index].density; // g/cm³
  density1.value = tableData.value[index].density; // g/cm³

  const thickness = tableData.value[index].thickness; // μm
  const pricePerGram = tableData.value[index].pricePerGram;
  pricePerGram1.value = pricePerGram;
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(
    `每平方米克重：${gramPerSquareMeter} g/m²`,
    density,
    thicknessInCm,
    thickness
  ); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike1.value = gramPerSquareMeter;
  caidanprice1.value = Number(roundedPrice);
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const caidanprice2 = ref(0); //材料单价
const caike2 = ref(0); //材料重量
const caa2 = (val: any) => {
  const density = Number(density2.value); // g/cm³
  const thickness = Number(thick.value.b); // μm
  const pricePerGram = Number(pricePerGram2.value);
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike2.value = gramPerSquareMeter;
  caidanprice2.value = Number(roundedPrice);
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const density2 = ref("");
const pricePerGram2 = ref("");
const ca2s = (val: any) => {
  const index = tableData.value.findIndex((item) => item.materialName === val);
  thick.value.b = tableData.value[index].thickness;
  density2.value = tableData.value[index].density; // g/cm³
  console.log("val", val, index);
  const density = tableData.value[index].density; // g/cm³
  const thickness = tableData.value[index].thickness; // μm
  const pricePerGram = tableData.value[index].pricePerGram;
  pricePerGram2.value = pricePerGram;
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike2.value = gramPerSquareMeter;
  caidanprice2.value = Number(roundedPrice);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const caidanprice3 = ref(0); //材料单价
const caike3 = ref(0); //材料重量
const caa3 = (val: any) => {
  const density = Number(density3.value); // g/cm³
  const thickness = Number(thick.value.c); // μm
  const pricePerGram = Number(pricePerGram3.value);
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike3.value = gramPerSquareMeter;
  caidanprice3.value = Number(roundedPrice);
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const density3 = ref("");
const pricePerGram3 = ref("");
const ca3s = (val: any) => {
  const index = tableData.value.findIndex((item) => item.materialName === val);
  thick.value.c = tableData.value[index].thickness;
  density3.value = thick.value.c;
  const density = tableData.value[index].density; // g/cm³
  const thickness = tableData.value[index].thickness; // μm
  const pricePerGram = tableData.value[index].pricePerGram;
  pricePerGram3.value = pricePerGram;
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike3.value = gramPerSquareMeter;
  caidanprice3.value = Number(roundedPrice);
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const caidanprice4 = ref(0); //材料单价
const caike4 = ref(0); //材料重量
const caa4 = (val: any) => {
  const density = Number(density4.value); // g/cm³
  const thickness = Number(thick.value.d); // μm
  const pricePerGram = Number(pricePerGram4.value);
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike4.value = gramPerSquareMeter;
  caidanprice4.value = Number(roundedPrice);
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const density4 = ref("");
const pricePerGram4 = ref("");
const ca4s = (val: any) => {
  const index = tableData.value.findIndex((item) => item.materialName === val);
  thick.value.d = tableData.value[index].thickness;
  density4.value = thick.value.d;
  const density = tableData.value[index].density; // g/cm³
  const thickness = tableData.value[index].thickness; // μm
  const pricePerGram = tableData.value[index].pricePerGram;
  pricePerGram4.value = pricePerGram;
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);

  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike4.value = gramPerSquareMeter;
  caidanprice4.value = Number(roundedPrice);
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};
const caidanprice5 = ref(0); //材料单价
const caike5 = ref(0); //材料重量
const caa5 = (val: any) => {
  const density = Number(density4.value); // g/cm³
  const thickness = Number(thick.value.e); // μm
  const pricePerGram = Number(pricePerGram5.value);
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);
  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike5.value = gramPerSquareMeter;
  caidanprice5.value = Number(roundedPrice);
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 4);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  addsT();
};
const density5 = ref("");
const pricePerGram5 = ref("");
const ca5s = (val: any) => {
  const index = tableData.value.findIndex((item) => item.materialName === val);
  thick.value.e = tableData.value[index].thickness;
  density5.value = thick.value.e;
  thick.value.ee = Number(tableData.value[index].pricePerKg) / 1000;
  const density = tableData.value[index].density; // g/cm³
  const thickness = tableData.value[index].thickness; // μm
  const pricePerGram = tableData.value[index].pricePerGram;
  pricePerGram5.value = pricePerGram;
  // 单位转换
  const thicknessInCm = thickness * 0.0001; // 25 μm → 0.0025 cm
  const areaInCm2 = 100 * 100; // 1 m² = 10,000 cm²
  // 计算每平方米的克重（g/m²）
  const gramPerSquareMeter = density * thicknessInCm * areaInCm2;
  console.log(`每平方米克重：${gramPerSquareMeter} g/m²`); // 16.75 g/m²

  // 计算每平方米的价格（元/m²）
  const pricePerSquareMeter = gramPerSquareMeter * pricePerGram;
  console.log(`每平方米价格：${pricePerSquareMeter} 元/m²`); // 0.210045 元/m²

  // 可选：四舍五入到 6 位小数
  const roundedPrice = truncateDecimals(pricePerSquareMeter, 6);

  console.log(`每平方米价格（四舍五入）：${roundedPrice} 元/m²`); // 0.210045 元/m²
  caike5.value = gramPerSquareMeter;
  caidanprice5.value = Number(roundedPrice);
  // 材料重量总和
  const caitatal =
    Number(caike1.value) +
    Number(caike2.value) +
    Number(caike3.value) +
    Number(caike4.value) +
    Number(caike5.value) +
    3 * Number(chen.value);
  console.log("计算得出材料总和", caitatal);
  const djs =
    Number(form.value.dm) * Number(caitatal) +
    Number(lake.value) +
    Number(qiKe.value) +
    Number(xiKe.value);
  form.value.dj = truncateDecimals(djs, 6);
  form.value.zl = Number(form.value.dj) * Number(form.value.num);
  //单只厚度
  form.value.dz =
    (Number(thick.value.a) +
      Number(thick.value.b) +
      Number(thick.value.c) +
      Number(thick.value.d) +
      Number(thick.value.e)) /
    1000;
  addsT();
};

const CloseAdd = () => {
  selectedValues.value = {
    daidan: "",
    yun: "",
    zong: "",
    fu: "",
  };
  matc.value = {
    daidan: "",
    yun: "",
    zong: "",
    fu: "",
  };
  form.value = {
    xishu: "",
    fujia: "",
    mj: "",
    dm: "",
    dj: "",
    dz: "",
    num: "",
    dk: "",
    dg: "",
    ms: "",
    ca1: "",
    ca2: "",
    ca3: "",
    ca4: "",
    ca5: "",
    zl: "",
    cd: "",
    djia: "", //单价
    lalian: "",
    xizui: "",
    qifa: "",
    fapiao: "",
    ke: "",
    tesu: "",
    fuhe: "",
    yinshua: "",
    daixing: "",
  };
  thick.value = {
    a: "",
    b: "",
    c: "",
    d: "",
    e: "",
  };
  TotalPrice.value = 0;
};
const factorData = ref([]);
const tableData = ref([]);
const listData = ref([]);
const dictDist = ref([]);
const productDataList = ref([]);
const daixing = ref([]);
const lalian = ref([]);
const qifa = ref([]);
const fuhe = ref([]);
const qizui = ref([]);
const tesugongyi = ref([]);
const kaipiao = ref([]);
const kaijifei = ref([]);
const yinshua = ref([]);
const ca1 = ref([]);
const tableDataJIANG = ref([]);
const fapiao = ref([]);
const GetPage = async () => {
  const tps = await dictList();
  tabs.value = tps.data.data;
  const treeDatas = tps.data.data;
  treeDatas.forEach((item) => {
    if (item.code == "DX") {
      daixing.value = item.dictDataList;
    }
    if (item.code == "LL") {
      lalian.value = item.dictDataList;
    }
    if (item.code == "QF") {
      qifa.value = item.dictDataList;
    }
    if (item.code == "XZ") {
      qizui.value = item.dictDataList;
    }
    if (item.code == "FH") {
      fuhe.value = item.dictDataList;
    }
    if (item.code == "TSGY") {
      tesugongyi.value = item.dictDataList;
    }
    if (item.code == "KP") {
      kaipiao.value = item.dictDataList;
    }
    if (item.code == "KJF") {
      kaijifei.value = item.dictDataList;
    }
    if (item.code == "YS") {
      yinshua.value = item.dictDataList;
    }
    if (item.code == "FHCL") {
      ca1.value = item.dictDataList;
    }
    if (item.code == "KP") {
      fapiao.value = item.dictDataList;
    }
  });
  const res = await list();
  tableData.value = res.data;

  const names = res.data.filter(
    (item) =>
      item.materialName.includes("降解") || item.materialName.includes("可回收")
  );
  tableDataJIANG.value = names;
  tableDataJIANG.value.unshift({ materialName: "无", value: "" });

  const dictres = await dict({ typeCode: "DXCC" });
  dictDist.value = dictres.data;
  const productData = await productprices({ pageNo: 1, pageSize: 1000 });
  const si = await bronzingList();
  productDataList.value = productData.data.data;

  si.data.forEach((item, index) => {
    listData.value.push({
      quantity: item.orderQuantity,
      printArea: item.printArea,
      logoColor: item.logoColor,
      printSides: item.printSides,
      totalPrice: item.totalPrice,
      unitPrice: item.unitPrice,
    });
  });
  const dd = await factorList({});
  factorData.value = dd.data;
};

onMounted(() => {
  GetPage();
});
const indexs = ref(0);
const chase = (val: any) => {
  const index = groupedData.value[selectedName.value].findIndex(
    (item) => item.size === val
  );
  indexs.value = index;
  console.log("----", groupedData.value[selectedName.value], "---", val, index);
  switch (val) {
    case "110*190*80":
      dictForm.value.houdu = "250g";
      break;
    case "85*190*60":
      dictForm.value.houdu = "100g";
      break;
    case "100*185*60":
      dictForm.value.houdu = "150g";
      break;
    case "100*190*75":
      dictForm.value.houdu = "200g";
      break;
    case "120*200*70":
      dictForm.value.houdu = "300g";
      break;
    case "127*200*80":
      dictForm.value.houdu = "340g";
      break;
    case "110*280*80":
      dictForm.value.houdu = "500g";
      break;
    case "190*200*80":
      dictForm.value.houdu = "500g";
      break;
    case "140*210*100":
      dictForm.value.houdu = "900g";
      break;
    case "140*345*95":
      dictForm.value.houdu = "1kg";
      break;
    case "180*415*105":
      dictForm.value.houdu = "2kg";
      break;
    case "130*210*80":
      dictForm.value.houdu = "100g";
      break;
    case "140*190*80":
      dictForm.value.houdu = "150g";
      break;
    case "150*220*80":
      dictForm.value.houdu = "200g";
      break;
    case "150*230*90":
      dictForm.value.houdu = "250g";
      break;
    case "170*240*80":
      dictForm.value.houdu = "300g";
      break;
    case "180*260*80":
      dictForm.value.houdu = "340g";
      break;
    case "180*290*100":
      dictForm.value.houdu = "500g";
      break;
    case "190*290*100":
      dictForm.value.houdu = "500g";
      break;
    case "230*340*100":
      dictForm.value.houdu = "800g";
      break;
    case "230*350*100":
      dictForm.value.houdu = "1kg";
      break;
    case "300*420*120":
      dictForm.value.houdu = "2kg";
      break;
    case "140*190":
      dictForm.value.houdu = "100g";
      break;
    case "150*220":
      dictForm.value.houdu = "150g";
      break;
    case "150*230":
      dictForm.value.houdu = "200g";
      break;
    case "180*260":
      dictForm.value.houdu = "250g";
      break;
    case "180*290":
      dictForm.value.houdu = "300g";
      break;
    case "180*310":
      dictForm.value.houdu = "340g";
      break;
    case "210*310":
      dictForm.value.houdu = "500g";
      break;
    case "210*350":
      dictForm.value.houdu = "500g";
      break;
    case "230*350":
      dictForm.value.houdu = "800g";
      break;
    case "260*350":
      dictForm.value.houdu = "1kg";
      break;
    case "350*420":
      dictForm.value.houdu = "2kg";
      break;
    default:
      break;
  }
  // dictForm.value.jg = groupedData.value[selectedName.value][index]
};

const Close = () => {
  dictForm.value = {
    dict: "",
    size: "",
    houdu: "",
    price: "",
    num: "",
    resPrice: "",
    thickness: "",
    jg: "",
    unit: "",
  };
  TotalPrice1.value = 0;
};

const SaveAdd = () => {
  const newVal = dictForm.value.num;
  if (newVal <= 1000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer1000Units;
  } else if (newVal <= 2000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer2000Units;
  } else if (newVal <= 3000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer3000Units;
  } else if (newVal <= 4000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer4000Units;
  } else if (newVal <= 5000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer5000Units;
  } else if (newVal <= 10000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  } else {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  }
  console.log(dictForm.value.jg, "sssss");
  const price = Number(dictForm.value.num) * Number(dictForm.value.jg);
  if (price < dictForm.value.price) {
    ElMessage({
      type: "warning",
      message: "最低起订金额不能低于" + dictForm.value.price + "元",
    });
    return;
  }

  TotalPrice1.value = price + Number(dictForm.value.resPrice);
};

const sizeData = ref([]);
const kafi = (val: any) => {
  console.log("groupedData", groupedData);
  // const index = groupedData.value[val].findIndex((item) => item.materialName === val)
  const newVal = dictForm.value.num;
  if (newVal <= 1000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer1000Units;
  } else if (newVal <= 2000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer2000Units;
  } else if (newVal <= 3000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer3000Units;
  } else if (newVal <= 4000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer4000Units;
  } else if (newVal <= 5000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer5000Units;
  } else if (newVal <= 10000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  } else {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  }
  if (dictForm.value.dict == "单侧拉链方底袋") {
    switch (val) {
      case "100g":
        dictForm.value.size = "85*190*60";
        break;
      case "150g":
        dictForm.value.size = "100*185*60";
        break;
      case "200g":
        dictForm.value.size = "100*190*75";
        break;
      case "250g":
        dictForm.value.size = "110*190*80";
        break;
      case "300g":
        dictForm.value.size = "120*200*70";
        break;
      case "340g":
        dictForm.value.size = "127*200*80";
        break;
      case "500g":
        dictForm.value.size = "110*280*80";
        break;
      case "500g":
        dictForm.value.size = "190*200*80";
        break;
      case "800g":
        dictForm.value.size = "140*210*100";
        break;
      case "1kg":
        dictForm.value.size = "140*345*95";
        break;
      case "2kg":
        dictForm.value.size = "180*415*105";
        break;
      default:
        break;
    }
  }
  if (dictForm.value.dict == "自立袋") {
    switch (val) {
      case "100g":
        dictForm.value.size = "130*210*80";
        break;
      case "150g":
        dictForm.value.size = "140*190*80";
        break;
      case "200g":
        dictForm.value.size = "150*220*80";
        break;
      case "250g":
        dictForm.value.size = "150*230*90";
        break;
      case "300g":
        dictForm.value.size = "170*240*80";
        break;
      case "340g":
        dictForm.value.size = "180*260*80";
        break;
      case "500g":
        dictForm.value.size = "180*290*100";
        break;
      case "500g":
        dictForm.value.size = "190*290*100";
        break;
      case "800g":
        dictForm.value.size = "230*340*100";
        break;
      case "1kg":
        dictForm.value.size = "230*350*100";
        break;
      case "2kg":
        dictForm.value.size = "300*420*120";
        break;
      default:
        break;
    }
  }
  if (dictForm.value.dict == "三边封拉链 ") {
    switch (val) {
      case "100g":
        dictForm.value.size = "140*190";
        break;
      case "150g":
        dictForm.value.size = "150*220";
        break;
      case "200g":
        dictForm.value.size = "150*230";
        break;
      case "250g":
        dictForm.value.size = "180*260";
        break;
      case "300g":
        dictForm.value.size = "180*290";
        break;
      case "340g":
        dictForm.value.size = "180*310";
        break;
      case "500g":
        dictForm.value.size = "210*310";
        break;
      case "500g":
        dictForm.value.size = "210*350";
        break;
      case "800g":
        dictForm.value.size = "230*350";
        break;
      case "1kg":
        dictForm.value.size = "260*350";
        break;
      case "2kg":
        dictForm.value.size = "350*420";
        break;
      default:
        break;
    }
  }

  dictForm.value.unit = truncateDecimals(dictForm.value.jg, 2);
};
const groupedDataClick = (val: any) => {
  selectedName.value = val;
  const index = groupedData.value[val].findIndex((item) => item.size === val);
  dictForm.value.price = groupedData.value[val][0].minOrderAmount;

  dictForm.value.thickness = groupedData.value[val][0].thickness;
  selectedItem.value = null;
  const newVal = dictForm.value.num;
  if (newVal <= 1000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer1000Units;
  } else if (newVal <= 2000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer2000Units;
  } else if (newVal <= 3000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer3000Units;
  } else if (newVal <= 4000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer4000Units;
  } else if (newVal <= 5000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer5000Units;
  } else if (newVal <= 10000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  } else {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  }
  dictForm.value.unit = truncateDecimals(dictForm.value.jg, 2);
};

// const uniqueNames = computed(() => {
//     return [...new Set(productDataList.value.map(item => item.materialName))];
// });

// console.log('uniqueNames',uniqueNames)
const totalNum = () => {
  console.log("输入框的值已更新", dictForm.value.num);
  if (dictForm.value.num == "") dictForm.value.unit = "";
  const newVal = dictForm.value.num;
  if (newVal <= 1000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer1000Units;
  } else if (newVal <= 2000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer2000Units;
  } else if (newVal <= 3000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer3000Units;
  } else if (newVal <= 4000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer4000Units;
  } else if (newVal <= 5000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer5000Units;
  } else if (newVal <= 10000) {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  } else {
    dictForm.value.jg =
      groupedData.value[selectedName.value][indexs.value].pricePer10000Units;
  }
  dictForm.value.unit = truncateDecimals(dictForm.value.jg, 2);
};
const calculateSum = () => {
  // 这里可以添加额外的逻辑
  console.log("输入框的值已更新", matc.value.daidan);
  if (matc.value.daidan) {
    // if (matchedData.value.unitPrice) {
    matc.value.zong =
      Number(matchedData.value.unitPrice) + Number(matc.value.daidan);
    // }
  } else {
    matc.value.zong =
      Number(matchedData.value.unitPrice) + Number(matc.value.daidan);
  }
};

interface BagData {
  pricePer1000Units: Number;
  pricePer2000Units: Number;
  materialName: String;
  pricePer3000Units: Number;
  pricePer4000Units: Number;
  pricePer5000Units: Number;
  pricePer10000Units: Number;
  coffeeBeanWeight: String;
  thickness: String;
  materialStructure: String;
  size: String;
  minOrderAmount: String;
}

const selectedName = ref<string>("");
// Selected Item
const selectedItem = ref<BagData | null>(null);
// 分组数据
const groupedData = computed(() => {
  return productDataList.value.reduce((acc, item) => {
    if (!acc[item.materialName]) {
      acc[item.materialName] = [];
    }
    acc[item.materialName].push(item);
    return acc;
  }, {} as Record<string, BagData[]>);
});
const dictForm = ref({
  dict: "",
  size: "",
  houdu: "",
  price: "",
  num: "",
  resPrice: "",
  thickness: "",
  jg: "",
  unit: "",
});

const matc = ref({
  daidan: "",
  yun: "",
  zong: "",
  fu: "",
});
const siwangClick = () => {
  if (matchedData.value?.unitPrice && matc.value.zong) {
    TotalPrice2.value =
      Number(matchedData.value.quantity) * Number(matc.value.zong) +
      Number(matc.value.fu) +
      Number(matc.value.yun);
    return;
  }
  ElMessage({
    type: "error",
    message: "请选择完整的选项以计算价格！",
  });
};

interface PrintData {
  quantity: number; // 起订量
  printArea: string; // 印刷区域
  logoColor: string; // logo颜色
  printSides: number; // 印刷面数
  totalPrice: number; // 印刷总价
  unitPrice: number; // 印刷单价
}
// 匹配的数据
const matchedData = ref<PrintData | null>(null);

// 更新价格
const updatePrice = () => {
  matc.value.zong = "";
  matc.value.daidan = "";
  if (
    selectedValues.value.quantity !== null &&
    selectedValues.value.printArea !== null &&
    selectedValues.value.logoColor !== null &&
    selectedValues.value.printSides !== null
  ) {
    const matched = listData.value.find(
      (item) =>
        item.quantity === selectedValues.value.quantity &&
        item.printArea === selectedValues.value.printArea &&
        item.logoColor === selectedValues.value.logoColor &&
        item.printSides === selectedValues.value.printSides
    );
    matchedData.value = matched || null;
  } else {
    matchedData.value = null;
  }
};

const selectedValues = ref<SelectedValues>({
  quantity: null,
  printArea: null,
  logoColor: null,
  printSides: null,
});

// 提取唯一值
const uniqueValues = computed(() => {
  return {
    quantity: [...new Set(listData.value.map((item) => item.quantity))],
    printArea: [...new Set(listData.value.map((item) => item.printArea))],
    logoColor: [...new Set(listData.value.map((item) => item.logoColor))],
    printSides: [...new Set(listData.value.map((item) => item.printSides))],
  };
});
</script>

<style scoped lang="scss">
.ipt {
  border: 1px solid #cec7c7;
  border-radius: 8px;
  padding: 25px;
  text-align: right;
  font-size: 25px;
  margin: 35px 0;
}
.btn {
  margin: 45px auto;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>